//这里判断是否有该用户对应该表的竞赛信息表，如果没有再点击报名的时候创建，
//如果如果有就改变报名状态，另外需要用备用字段1作为提交状态
//定义一个状态判断是否是已经存在竞赛信息表的数据，statuss

var application=document.querySelector('#application');//报名按钮
var cancel=document.querySelector('#Cancel');//取消报名按钮
var spApp=document.querySelector('#spApp');//报名块
var spCan=document.querySelector('#spCan');//取消报名块
//这是计时器
var sp=document.querySelector('#timer_span');
function daojishi() {
    var endTime=contestBeginTime;//将时间转化的跟数据库一致
    var inter=setInterval(function () {
        var newTime=new Date();
        var rtime=endTime-newTime;
        var f=get(rtime);
        sp.innerHTML=f;
    },500);
}
function get(time) {
    var s=Math.floor((time/1000)%60);
    // console.log(s);
    var m=Math.floor((time/1000/60)%60);
    // console.log(m);
    var h=Math.floor((time/1000/3600)%24);
    // console.log(h);
    var d=Math.floor(time/1000/3600/24)
    var jieguo=d+'天'+h+'小时'+m+'分钟'+s+'秒';
    return jieguo;
}
//在比赛的时间阶段内
var beginContestModal = document.querySelector("#beginModal");//开始
var endContestModal = document.querySelector("#endModal");//结束
var contestEnd = document.querySelector("#contestEnd");
var contestBegin = document.querySelector("#contestBegin");
var nowTime=new Date().getTime();

//比赛时间以内
if(nowTime>contestBeginTime && nowTime < contestEndTime){
    spApp.style.display="none";//让报名跟取消报名隐藏，换上开始竞赛与结束竞赛
    spCan.style.display="none";
    sp.parentElement.innerText = "本场竞赛已经开始";
    var change=true;
    function contestRunning() {
        var ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajax.open("GET",ROOTSources+"/contest/getContestData/"+contestId,true);
        ajax.send();
        ajax.onreadystatechange=function () {
            if (ajax.readyState === 4) {
                if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                    var result = JSON.parse(ajax.responseText);
                    contestEnd.innerHTML += `<div class="row" style="margin-top: 30px">
                    <div class="col-md-6">
                        <ul class="list-group hover-panel contest-question-list">
                            <li class="list-group-item heading">
                                题目列表
                                <span class="glyphicon glyphicon-question-sign pull-right" data-toggle="tooltip" data-placement="top" aria-hidden="true" id="questionMark"></span>
                                <span class="pull-right">得分&nbsp;&nbsp;</span>
                            </li>
                            <li class="list-group-item">
                                <a href=${ROOTSources+"/contest/goContestCompile/"+result.data[0].programId}>${result.data[0].programTitle}</a>
                                <span class="badge">5</span>
                            </li>
                            <li class="list-group-item">
                                <a href=${ROOTSources+"/contest/goContestCompile/"+result.data[1].programId}>${result.data[1].programTitle}</a>
                                <span class="badge">5</span>
                            </li>
                            <li class="list-group-item">
                                <a href=${ROOTSources+"/contest/goContestCompile/"+result.data[2].programId}>${result.data[2].programTitle}</a>
                                <span class="badge">5</span>
                            </li>
                        </ul>
                    </div>
                    <div class="col-md-6">
                        <div class="panel panel-default hover-panel ranking-list">
                            <div class="panel-heading">
                                本场排名
                            </div>
                            <table class="table table-hover table-striped">                              
                                <tbody>
                                <tr>
                                    <td>用户名</td>
                                    <td>得分</td>
                                    <td>
                                        <span>完成时间</span>
                                    </td>
                                </tr>
                                <tr>
                                    等待成绩更新
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div id="tips"><span>每个题对应的分值</span></div>`
                }
            }
        };
        if (change === true){
            contestBegin.style.display="none";
            contestEnd.style.display="block";
            change=false;
        }
        return change;
    }

    window.onload=function(){
        //判断是否提交过代码,或者报名
        var ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajax.open("GET",ROOTSources+"/contest/determineContestStatus/"+contestId+"/"+customerId,true);
        ajax.send();
        ajax.onreadystatechange=function () {
            if (ajax.readyState === 4) {
                if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                    var result=JSON.parse(ajax.responseText);
                    if (result.data.contestMsgStartTime !=null && result.data.contestMsgThroughTime == null){
                        contestRunning();
                    }else if (result.data.contestMsgMark != null || result.data.applyStatus !== "1") {
                        alert("检测到您已经提交代码或未报名，现在将返回竞赛主页！点击确定返回");
                        window.history.go(-1)
                    }
                }
            }
        };
        //判断是否报名，未报名不能够参加竞赛

    };
    //点击开始竞赛按钮，需要刷新页面，下方显示题目列表
    beginContestModal.onclick=function(){
        //先显示出题目列表
        contestRunning();
        //这里要向后台发送一个开始时间
        var ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajax.open("GET",ROOTSources+"/contest/sendStartTime/"+contestId+"/"+customerId,true);
        ajax.send();
        ajax.onreadystatechange=function () {
            if (ajax.readyState === 4) {
                if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                    var result=JSON.parse(ajax.responseText);
                }
            }
        };
    };
    endContestModal.onclick=function () {
        var ajax = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajax.open("GET",ROOTSources+"/contest/endContest/"+contestId+"/"+customerId,true);
        ajax.send();
        ajax.onreadystatechange=function () {
            if (ajax.readyState === 4) {
                if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                    var result=JSON.parse(ajax.responseText);
                    console.log(result.message);
                    alert("您已结束竞赛");
                    setTimeout(window.location.href=ROOTSources+"/competitionPage",2000);
                    if (change === false){
                        contestBegin.style.display="block";
                        contestEnd.style.display="none";
                        change=true;
                    }
                    return change;
                }
            }
        };
    }
}
//比赛时间以外
else {
    contestEnd.style.display="none";//隐藏开始按钮
    contestBegin.style.display="none";//隐藏结束按钮
    var flag=true;// 报名状态改变
    window.onload=function () {
        var ajax =new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajax.open("GET",ROOTSources+"/contest/getContestMessage/"+contestId+"/"+customerId,true);
        ajax.send();
        ajax.onreadystatechange=function() {
            if (ajax.readyState === 4) {
                if (ajax.status >= 200 && ajax.status < 300 || ajax.status === 304) {
                    var result = JSON.parse(ajax.responseText);
                    var applyStatus = result.data.applyStatus;
                    daojishi();
                    if (applyStatus===1 && result.status !== 0){
                        spCan.style.display='block';
                        spApp.style.display='none';
                        flag=false;
                    }
                    return flag;
                }
            }
        }
    };//页面加载事件  判断该用户是否报名来显示页面
    //判断是否建立竞赛信息表，并控制点击报名的事件
    application.onclick=function (){//当现实报名时
        var ajax =new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        ajax.open("GET",ROOTSources+"/contest/getContestMessageTable/"+contestId+"/"+customerId,true);
        ajax.send();
        ajax.onreadystatechange=function(){
            if (ajax.readyState ===4){
                if (ajax.status >= 200 && ajax.status<300 || ajax.status===304){
                    var result = JSON.parse(ajax.responseText);
                    alert("您已报名");
                    spCan.style.display='block';
                    spApp.style.display='none';
                    flag=false;
                    return flag;
                }
            }
        };
    };
    cancel.onclick=function () {
        var modifyAjax =  new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTTP');
        modifyAjax.open("GET",ROOTSources+"/contest/getContestMessageTable/"+contestId+"/"+customerId,true)
        modifyAjax.send();
        modifyAjax.onreadystatechange = function () {
            if (modifyAjax.readyState ===4){
                if (modifyAjax.status>=200 && modifyAjax.status<300 || modifyAjax.status===304){
                    var result1 = JSON.parse(modifyAjax.responseText);
                    setTimeout(history.go(0),2000);
                }
            }
        };
        if(flag===false){
            spCan.style.display='none';
            spApp.style.display='block';
            flag=true
        }
        return flag;
    }; //当显示取消报名时
}